﻿using Power.View.Dialog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.ToolTip;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight;
using Power.Models;
using System.Collections.ObjectModel;
using Power.View;
using System.Windows.Controls.Primitives;
using Power.Common;
using HandyControl.Controls;
using System.Windows.Forms;
using System.Windows.Markup;

namespace Power.ViewModel.Dialog
{
    public class PleaseModifyViewModel : ViewModelBase
    {
        private PleaseModify model;
        public PleaseModifyViewModel(PleaseModify model)
        {
            this.model = model;
            Close = new RelayCommand(OnClose);
            CloseOK = new RelayCommand(OnCloseOK);
            ErroList = new List<ErrorItem>() {
                new ErrorItem() { Row = 1, Column = 2, Message = "未填写" },
                new ErrorItem() { Row = 1, Column = 5, Message = "填写有误" }
            };
            Data = new PleaseModifyViewModelEntity()
            {
                DetectRecordEntity = new ObservableCollection<DetectRecordEntity>()
                {
                    new DetectRecordEntity()
                    {
                        ID = 1,
                        Area = "区域",
                        Block = "区块",
                        WorkField = "工点",
                        StartWell = new WellEntity()
                        {
                            No = "2",
                            Type = "1",
                            FuncType = "1",
                            Mat = "1",
                            Problem = "1",
                        },
                        EndWell = new WellEntity()
                        {

                            No = "2",
                            Type = "1",
                            FuncType = "1",
                            Mat = "1",
                            Problem = "1",
                        },
                        Defect = new DefectEntity()
                        {
                            No = 1,
                            Type = "1",
                            Name = "1",
                            Level = 1,
                            Length = 1,
                            CircleDirection = "1",
                            DetectDate = "1",
                            StartPos = 1,
                            EndPos = 1,
                            Code = "1",
                            Describe = "1",
                        },
                        Detect = new DetectEntity() {
                            Length = 1,
                            Method = "1",
                            Direction = "1",
                            Date = "1",
                            Batch = 1,
                            Detector = "1",
                            Subcontractors = "1",
                            PicName = "1",
                            Describe = "1",
                            WorkGroup = "1",
                            CollectDetector = "1"
                        },
                        Pip = new PipEntity()
                        {
                            No = 1,
                            BeginBuriedDepth = 1,
                            EndBuriedDepth = 9,
                            Type = "1",
                            Mat = "1",
                            Diameter = 1,
                            Length = 1,
                            AreaParam = "1",
                            SolidParam = "1",
                            BuildDate = "1",


                        }
                    },
                    new DetectRecordEntity()
                    {
                        ID = 2,
                        Area = "区域",
                        Block = "区块",
                        WorkField = "工点",
                        StartWell = new WellEntity()
                        {
                            No = "2",
                            Type = "1",
                            FuncType = "1",
                            Mat = "1",
                            Problem = "1",

                        },
                        EndWell = new WellEntity()
                        {

                            No = "2",
                            Type = "1",
                            FuncType = "1",
                            Mat = "1",
                            Problem = "1",
                        },
                        Defect = new DefectEntity()
                        {
                            No = 1,
                            Type = "1",
                            Name = "1",
                            Level = 1,
                            Length = 1,
                            CircleDirection = "1",
                            DetectDate = "1",
                            StartPos = 1,
                            EndPos = 1,
                            Code = "1",
                            Describe = "1",
                        },
                        Detect = new DetectEntity() {
                            Length = 1,
                            Method = "1",
                            Direction = "1",
                            Date = "1",
                            Batch = 1,
                            Detector = "1",
                            Subcontractors = "1",
                            PicName = "1",
                            Describe = "1",
                            WorkGroup = "1",
                            CollectDetector = "1"
                        },
                        Pip = new PipEntity()
                        {
                            No = 1,
                            BeginBuriedDepth = 1,
                            EndBuriedDepth = 9,
                            Type = "1",
                            Mat = "1",
                            Diameter = 1,
                            Length = 1,
                            AreaParam = "1",
                            SolidParam = "1",
                            BuildDate = "1",
                        }
                    }

                },
                ErrorList = InitErrorItem(ErroList)
            };
        }

        public void SetErrorStyle(List<ErrorItem> list) {
            foreach (var item in list)
            {
                System.Windows.Controls.DataGridCell cell = this.model.grid.GetCell(item.Row - 1, item.Column);
                cell.Background = new SolidColorBrush(Colors.Red);
                cell.Foreground = new SolidColorBrush(Colors.White);
            }
        }

        public ObservableCollection<string> InitErrorItem(List<ErrorItem> list) {
            ObservableCollection<string> result = new ObservableCollection<string>();
            foreach (var item in list)
            {
                result.Add($"第{item.Row}行 第{item.Column}列：单元格数据{item.Message}");
            }
            return result;
        }

        private void OnCloseOK()
        {
            this.model.DialogResult = true;
            this.model.Close();
        }

        private void OnClose()
        {
            this.model.DialogResult = false;
            this.model.Close();
        }

        #region 
        public RelayCommand Close { get; private set; }
        public RelayCommand CloseOK { get; private set; }
        #endregion
        #region 数据绑定
        public PleaseModifyViewModelEntity Data { get; private set; }

        private DetectRecordEntity selectedItem;
        public DetectRecordEntity SelectedItem
        {
            get
            {
                return selectedItem;
            }
            set
            {
                selectedItem = value;
                RaisePropertyChanged();
            }
        }

        private bool checkBoxIsChecked;
        public bool CheckBoxIsChecked
        {
            get
            {
                return checkBoxIsChecked;
            }
            set
            {
                checkBoxIsChecked = value;
                ChangedSelecgedFlags();
                RaisePropertyChanged();
                RaisePropertyChanged("Data.DetectRecordEntity");
            }
        }

        private void ChangedSelecgedFlags()
        {
            foreach (var e in Data.DetectRecordEntity)
                e.IsSelectedFlag = CheckBoxIsChecked;
        }

        public List<ErrorItem> ErroList;

        public class ErrorItem{
            public int Row { get; set; }

            public int Column { get; set; }

            public String Message { get; set; }
        }
        #endregion
    }
}
